<template>
  <h1>药品信息</h1>
  <el-row :gutter="10">

    <el-col :span="6">
      <el-input v-model="query.MedicineData.medicineName" placeholder="请输入药品名称" style="width: 200px;" />
    </el-col>

    <el-col :span="6">
      <el-select v-model="query.MedicineData.medicineType"
                 :clearable="true"
                 placeholder="请选择药物类型">
        <el-option v-for="item in medicineTypes.data"
                   :key="item.medicineType"
                   :label="item.medicineType"
                   :value="item.medicineType" />
      </el-select>
    </el-col>

    <el-col :span="6">
      <el-button type="primary" @click="getMedicineData(query.MedicineData)">搜索</el-button>
    </el-col>

  </el-row>
  <el-table :data="tableData.data" style="width: 100%;margin-top: 20px">

    <el-table-column label="序号" type="index" width="80" align="center">
    </el-table-column>

    <el-table-column label="名称" prop="medicineName" width="80" align="center">
    </el-table-column>

    <el-table-column label="类型" prop="medicineType" width="100" align="center">
    </el-table-column>


    <el-table-column label="库存" prop="number" width="100" align="center">
      <template #default="scope">
        <div>{{scope.row.number+scope.row.unit}}</div>
      </template>
    </el-table-column>


    <el-table-column label="价格" prop="price" width="100" align="center">

      <template #default="scope">
        <div>{{(scope.row.price/100).toFixed(2)+'元'}}</div>
      </template>
    </el-table-column>

    <el-table-column label="操作" width="200px" align="center">
      <template #default="scope">
        <el-button
          style="width: 60px;"
          size="small"
          @click="handleEdit(scope.row)"
        >修改
        </el-button>
        <el-button
          style="width: 60px;"
          size="small"
          type="danger"
          v-model:current-page="query.MedicineData.pages"
          v-model:age-sizes="query.MedicineData.size"
          @click="handleDelete(scope.row)"
        >删除
        </el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <!--分页组件-->
  <div>
    <el-pagination
      small
      layout="total, prev, pager, next, jumper"
      :total="tableData.total"
      class="pagiantion "
      @current-change="pageChange"
      v-model:current-page="query.MedicineData.pages"
      v-model:page-size="query.MedicineData.size"
    />
  </div>
</template>

<script lang="js" setup>
import { onMounted, reactive } from "vue";
import { getMedicine, deleteMedicine, getMedicineType } from "@/utils/MedicineRequest";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
import { getDepartmentNameId } from "@/utils/api";
const router = useRouter();
//展示数据
const tableData = reactive({
  data: [],
  total: 0,
  current: 1, //当前页数
  size: 10  //每页显示几条数据
});

//请求参数
const query = reactive({
  MedicineData: {
    "medicineName": "",
    "medicineType": "",
    "size": tableData.size,
    "pages": tableData.current,
  }
});

const medicineTypes = reactive({
  data: []
});

//组件加载时触发
onMounted(() => {
  getMedicineData(query.MedicineData);
  getMedicineTypeOptionData();
});

//获取药品类别
const getMedicineTypeOptionData = async () => {
  let res = await getMedicineType();
  medicineTypes.data = res.data;
};

//获取数据
const getMedicineData = async (requestData) => {
  let res = await getMedicine(requestData);
  tableData.data = res.data;
  tableData.total = res.total;
  tableData.current = res.current;
};

//分页的页码变化时触发
const pageChange = () => {
  getMedicineData(query.MedicineData);
};

//修改回调
const handleEdit = (row) => {
  router.push({
    path:'/home/updateMedicine',
    query:{id:row.medicineId}
  })
};

//删除
const handleDelete = (row) => {
  ElMessageBox.confirm(
    "确定要删除吗?",
    "警告",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning"
    }
  )
    .then(async () => {
      let res = await deleteMedicine(row.medicineId);
      if (res.data) {
        ElMessage({
          type: "success",
          message: "删除成功"
        });
      } else {
        ElMessage({
          type: "error",
          message: "删除成功"
        });
      }
      getMedicineData(query.MedicineData);

    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消删除"
      });
    });
};

</script>

<style scoped>
</style>